package com.sxj.PDMS.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxj.PDMS.common.ErrorCode;
import com.sxj.PDMS.exception.BusinessException;
import com.sxj.PDMS.mapper.FullPartyMemberInformationListMapper;
import com.sxj.PDMS.model.dto.admin.PartyMemberAwardsRequest;
import com.sxj.PDMS.model.entity.FullPartyMemberInformationList;
import com.sxj.PDMS.model.entity.Message;
import com.sxj.PDMS.model.entity.PartyMemberAwards;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.PartyMemberAwardsService;
import com.sxj.PDMS.mapper.PartyMemberAwardsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.sxj.PDMS.utils.VerifyUtil.formatDate;

/**
* @author 503c'j
* @description 针对表【party_member_awards(党员奖励表)】的数据库操作Service实现
* @createDate 2024-06-16 17:26:29
*/
@Service
public class PartyMemberAwardsServiceImpl extends ServiceImpl<PartyMemberAwardsMapper, PartyMemberAwards>
    implements PartyMemberAwardsService{
    @Autowired
    private FullPartyMemberInformationListMapper fullPartyMemberInformationListMapper;
    @Autowired
    private PartyMemberAwardsMapper partyMemberAwardsMapper;
    @Override
    public Boolean awardsUpload(PartyMemberAwardsRequest partyMemberAwardsRequest) throws ParseException {
        FullPartyMemberInformationList fullPartyMemberInformationList = fullPartyMemberInformationListMapper.selectById(partyMemberAwardsRequest.getPartyMemberNumber());
        if(fullPartyMemberInformationList == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, partyMemberAwardsRequest.getPartyMemberNumber() + "的党员不存在");
        }
        PartyMemberAwards partyMemberAwards = new PartyMemberAwards();
        BeanUtils.copyProperties(partyMemberAwardsRequest, partyMemberAwards);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        partyMemberAwards.setAwardDate(formatDate(currentTime));
        boolean saved = this.save(partyMemberAwards);
        return saved;
    }

    @Override
    public PagingQueryResponse<PartyMemberAwards> awardsSelect(Integer page, Integer pageSize, PartyMemberAwards partyMemberAwards) {
        Integer start = (page - 1) * pageSize;
        String bonusContent = partyMemberAwards.getBonusContent();
        Long partyMemberNumber = partyMemberAwards.getPartyMemberNumber();
        Date awardDate = partyMemberAwards.getAwardDate();

        List<PartyMemberAwards> partyMemberAwardsList = partyMemberAwardsMapper.selectByContentOrMemberNumberOrDate(start, pageSize, bonusContent,partyMemberNumber, awardDate);
        Long total = partyMemberAwardsMapper.selectTotalByContentOrMemberNumberOrDate( bonusContent,partyMemberNumber, awardDate);

        PagingQueryResponse<PartyMemberAwards> pagingQueryResponse = new PagingQueryResponse<>();
        pagingQueryResponse.setRows(partyMemberAwardsList);
        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

    @Override
    public PagingQueryResponse<PartyMemberAwards> selectAll(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<PartyMemberAwards> partyMemberAwardsList = partyMemberAwardsMapper.selectAll(start, pageSize);
        Long total = partyMemberAwardsMapper.selectTotal();
        PagingQueryResponse<PartyMemberAwards> pagingQueryResponse = new PagingQueryResponse<>();

        pagingQueryResponse.setRows(partyMemberAwardsList);

        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }


}




